package com.luck.emp.cust.logic;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;

import com.luck.emp.cust.qry.CustQry;
import com.luck.emp.entity.EmpTCustInfo;
import com.luck.emp.entity.EmpTExpress;
import com.luck.emp.entity.EmpTSalesman;
import com.luck.pf.common.entity.UserBean;
import com.luck.pf.framework.dao.BaseJdbcDao;
import com.luck.pf.framework.logic.BaseLogic;
import com.luck.pf.util.CriteriaUtil;
import com.luck.pf.util.DateUtil;
import com.luck.pf.util.Page;

/**
 * 说明 Copyright: Copyright (c) 2011 深圳**科技
 * 
 * @author
 * 
 */
public class CustLogic extends BaseLogic implements ICustLogic {
	private BaseJdbcDao jdbcDao;

	/**
	 * 查找分页信息
	 */
	public Page findCustPage(CustQry qry) {
		Criteria criteria = baseDao.createCriteria(EmpTCustInfo.class);
		CriteriaUtil.addEq(criteria, "custCode", qry.getCustCode());
		CriteriaUtil.addEq(criteria, "custName", qry.getCustName());
		CriteriaUtil.addEq(criteria, "contactName", qry.getContactName());
		CriteriaUtil.addEq(criteria, "contactTel", qry.getContactTel());
		CriteriaUtil.addEq(criteria, "custStatus", qry.getStatus());
		Order order = Order.desc("id");
		criteria.addOrder(order);
		if(qry.getOrgId() != null)
			CriteriaUtil.addEq(criteria, "deptId", qry.getOrgId());
		else{
			criteria.createAlias("dept", "d");
			filterData(criteria, qry.getUserInfo(), "d", null);
		}
		return baseDao.pagedQuery(criteria, qry.getPageNo(), qry.getPageSize());
	}
	private EmpTCustInfo findCust(Long deptId, String custCode){
		List<EmpTCustInfo> ls = super.baseDao.find(" FROM EmpTCustInfo WHERE custCode=? and deptId=?",new Object[]{custCode.trim(), deptId});
		if(ls !=null && !ls.isEmpty())
			return ls.get(0);
		return null;
	}
	/**
	 * 保存
	 **/
	public String saveCust(UserBean userBean, EmpTCustInfo entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		// 检查保存逻辑
		if (entity.getId() == null) {
			entity.setCreateUser(userBean.getUser());
			entity.setCreateDate(DateUtil.getDate());
			entity.setDeptId(userBean.getOrgId());
		}
		entity.setModiUser(userBean.getUser());
		entity.setModiDate(DateUtil.getDate());
		// 如果保存逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if(findCust(userBean.getDeptId(), entity.getCustCode())!=null)
			bf.append("此客户编码已经存在");
		if (StringUtils.isBlank(bf.toString())) {
			super.save(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	/**
	 * 删除
	 */
	public String deleteCust(UserBean userBean, EmpTCustInfo entity)
			throws Exception {
		StringBuffer bf = new StringBuffer();
		// 检查删除逻辑
		int count = super.baseDao.count("  select count(*) from EmpTSlOrderM t where custId = ?",entity.getId());
		if (count > 0)
			bf.append("<br>此客户已经有销售业务数据");
		int count2 = super.baseDao.count("  select count(*) from EmpTWhOrderM t where custInfoId = ?",entity.getId());
		if (count2 > 0)
			bf.append("<br>此客户已经有出入库订单数据");

		entity = super.get(EmpTCustInfo.class, entity.getId());
		if(!entity.getDeptId().equals(userBean.getDeptId()))
			bf.append("<br>不属于您所在的机构");
		// 如果删除逻辑不通过则会在BF中增加提示信息，如不为空，则抛出错误提示。
		if (StringUtils.isBlank(bf.toString())) {
			super.baseDao.delete(entity);
		} else
			throw new Exception(bf.toString());
		return bf.toString();
	}

	public BaseJdbcDao getJdbcDao() {
		return jdbcDao;
	}

	public void setJdbcDao(BaseJdbcDao jdbcDao) {
		this.jdbcDao = jdbcDao;
	}

}